﻿using PTop.Application.Biz;
using PTop.Code;
using PTop.Domain.Entity.Questionnaire;
using PTop.Domain.IRepository.Questionnaire;
using PTop.Repository.Questionnaire;
using System.Collections.Generic;

namespace PTop.Application.Questionnaire
{
    /// <summary>
    /// 调查问卷答题App
    /// </summary>	
    public class QnAnswerApp
    {
        private IQnAnswerRepository service = new QnAnswerRepository();

        public List<QnAnswerEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<QnAnswerEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                //expression = expression.And(t => t.F_OrgnaizeName.Contains(keyword));
                //expression = expression.Or(t => t.F_CreatorUserCode.Contains(keyword));
            }
            return service.FindList(expression, pagination);
        }
        public List<QnAnswerEntity> GetListByPaper(string paperId)
        {
            var expression = ExtLinq.True<QnAnswerEntity>();
            expression = expression.And(t => t.F_PaperId == paperId);
            var p = new Pagination
            {
                page = 1,
                rows = 10000,
                sidx = "F_CreatorTime desc",
                sord = "asc"
            };
            return service.FindList(expression, p);
        }
        public List<QnAnswerEntity> GetListByQuestion(string questionId)
        {
            var expression = ExtLinq.True<QnAnswerEntity>();
            expression = expression.And(t => t.F_QuestionId == questionId);
            var p = new Pagination
            {
                page = 1,
                rows = 10000,
                sidx = "F_CreatorTime desc",
                sord = "asc"
            };
            return service.FindList(expression, p);
        }
        public List<QnAnswerEntity> GetListByUser(string openId, string paperId)
        {
            var expression = ExtLinq.True<QnAnswerEntity>();
            expression = expression.And(t => t.F_CreatorUserId == openId);
            expression = expression.And(t => t.F_PaperId == paperId);
            var p = new Pagination
            {
                page = 1,
                rows = 1000,
                sidx = "F_CreatorTime desc",
                sord = "asc"
            };
            return service.FindList(expression, p);
        }
        public QnAnswerEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }
        public void DeleteByPaperId(string paperId)
        {
            service.Delete(t => t.F_PaperId == paperId);
        }

        public void SubmitForm(QnAnswerEntity entity, string keyValue)
        {
            var question = new QnQuestionApp().GetForm(entity.F_QuestionId);
            entity.UpdateOptions(question.F_Type);
            if (!string.IsNullOrEmpty(keyValue))
            {
                service.Update(entity);
            }
            else
            {
                entity.Create();
                service.Insert(entity);
                new UserStatisticsApp().AddCount("F_QnAnswerCount");
            }
        }

        public void AddAnswers(List<QnAnswerEntity> entities)
        {
            foreach (var entity in entities)
            {
                entity.F_Id = Common.GuId();
            }
            service.Insert(entities);
        }
    }
}